package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.mapper.EntGoalSummaryMapper;
import com.prd.model.EntGoalSummary;
import com.prd.model.EntGoalSummaryFile;
import com.prd.model.LoginUser;
import com.prd.service.EntGoalSummaryFileService;
import com.prd.service.EntGoalSummaryService;
import com.prd.service.UserService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EntGoalSummaryServiceImpl extends ServiceImpl<EntGoalSummaryMapper, EntGoalSummary> implements EntGoalSummaryService {

    @Autowired
    private EntGoalSummaryMapper entGoalSummaryMapper;

    @Autowired
    private EntGoalSummaryFileService entGoalSummaryFileService;

    @Autowired
    private UserService userService;

    @Override
    public int insertInfo(EntGoalSummary entGoalSummary) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entGoalSummary);
        entGoalSummaryFileService.saveBatch(entGoalSummary.getListFile());
        return entGoalSummaryMapper.insert(entGoalSummary);
    }

    @Override
    public int updateInfo(EntGoalSummary entGoalSummary) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entGoalSummary);
        return entGoalSummaryMapper.updateById(entGoalSummary);
    }

    @Override
    public List<EntGoalSummary> listInfo(EntGoalSummary entGoalSummary) {
        LambdaQueryWrapper<EntGoalSummary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.setEntity(entGoalSummary);
        List<EntGoalSummary> list = entGoalSummaryMapper.selectList(queryWrapper);
        for (EntGoalSummary summary1 : list) {
            summary1.setInitiator(userService.listName(Long.parseLong(summary1.getInitiator())).getMerge());
            summary1.setParticipant(userService.listName(Long.parseLong(summary1.getParticipant())).getMerge());
            summary1.setCompile(userService.listName(Long.parseLong(summary1.getCompile())).getMerge());
            summary1.setMake(userService.listName(Long.parseLong(summary1.getMake())).getMerge());
        }
        return list;
    }

    @Override
    public EntGoalSummary details(Long id) {
        EntGoalSummary entGoalSummary = entGoalSummaryMapper.selectById(id);
        LambdaQueryWrapper<EntGoalSummaryFile> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntGoalSummaryFile::getInterviewId, id);
        entGoalSummary.setListFile(entGoalSummaryFileService.list(queryWrapper));
        return entGoalSummary;
    }
}
